package com.avic.rong.goodsSort.fragment.bz.secondfragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.R;
import com.avic.rong.fragment.BaseLazyFragment;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.adapter.GoodsBzTabRvAdapter;
import com.avic.rong.goodsSort.api.GoodsSortZbApi;
import com.avic.rong.goodsSort.bean.CatChildBean;
import com.avic.rong.goodsSort.bean.GoodsSortZbBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.avic.rong.wedgit.treeRvAdapter.FilterAdapter;
import com.avic.rong.wedgit.treeRvAdapter.FilterDao;
import com.avic.rong.wedgit.treeRvAdapter.ItemStatus;
import com.avic.rong.wedgit.treeRvAdapter.OptionDao;
import com.avic.rong.wedgit.treeRvAdapter.TreeListDao;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;

import java.lang.reflect.Type;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * 中标分类
 */
public class GoodsBiaoZhunZBFragment extends BaseLazyFragment {



    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;
    @BindView(R.id.recycler_list)
    RecyclerView recycler_list;

    private List<CatChildBean>  beanList;
    private GoodsBzTabRvAdapter adapter;
    private FilterAdapter filterAdapter;
    private List<TreeListDao<FilterDao, OptionDao>> treeList = new ArrayList<>();
    public String currentSubType = "bz";

    public GoodsBiaoZhunZBFragment() {
        // Required empty public constructor
    }

    public static GoodsBiaoZhunZBFragment getInstance(String subType){
        GoodsBiaoZhunZBFragment zhunZBFragment = new GoodsBiaoZhunZBFragment();
        zhunZBFragment.currentSubType = subType;
        return zhunZBFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_biao_zhun_zb, container, false);
        ButterKnife.bind(this, view);
        loading_layout.hideReturnButton();
        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loading_layout.setStatus(LoadingLayout.LOADING);
                loadData();
            }

            @Override
            public void onReturn(View v) {

            }
        });

        return view;
    }


    @Override
    public void loadData() {
        GoodsSortZbApi goodsSortZbApi = new GoodsSortZbApi((GoodsSortActivity)getContext(), delCartListener, 1,currentSubType);
        goodsSortZbApi.setCache(true);
        HttpManager.getInstance().doHttpDeal(goodsSortZbApi);
    }


    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            if (!ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())){
                ToastTools.showToast(getContext(),subjects.getMsg());
                return;
            }
            GoodsSortZbBean bean = (GoodsSortZbBean) subjects.getData();
            if ("gjb05".equals(currentSubType) || "gjb90".equals(currentSubType)){//如果是gjb05或gjb90则解析深一层级
                if (bean.getInternalList().size() > 0){
                   beanList = bean.getInternalList().get(0).getChilds();
                }
            }else{
                beanList = bean.getInternalList();
            }

            setAdapter();
        }

        @Override
        public void onCacheNext(String cache) {
            super.onCacheNext(cache);
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            Gson gson = new Gson();
            Type type = new TypeToken<BaseResultEntity<GoodsSortZbBean>>(){}.getType();
            BaseResultEntity resultEntity = gson.fromJson(cache, type);
            GoodsSortZbBean bean = (GoodsSortZbBean)resultEntity.getData();
            if ("gjb05".equals(currentSubType) || "gjb90".equals(currentSubType)){//如果是gjb05或gjb90则解析深一层级
                if (bean.getInternalList().size() > 0){
                    beanList = bean.getInternalList().get(0).getChilds();
                }
            }else{
                beanList = bean.getInternalList();
            }
            setAdapter();

        }

        @Override
        public void onError(final Throwable e) {
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
            Logger.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };

    private void setAdapter() {


        filterAdapter = new FilterAdapter(getContext(), treeList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                if (filterAdapter.getItemViewType(i) == ItemStatus.VIEW_TYPE_SUB_ITEM){
                    return 1;
                }
                return  2;
            }
        });

        recycler_list.setLayoutManager(gridLayoutManager);
        recycler_list.setAdapter(filterAdapter);
        filterAdapter.setData(initData());

    }

    private List<TreeListDao<FilterDao, OptionDao>> initData(){

        List<TreeListDao<FilterDao, OptionDao>> dataList = new ArrayList<>();

        for(CatChildBean childBean : beanList){
            TreeListDao<FilterDao, OptionDao> daoInternal = new TreeListDao<FilterDao, OptionDao>();
            FilterDao filterDaoTwoInternal = new FilterDao(childBean.getCatId(),childBean.getCatNameShow(),childBean.getCatNameShow(),null,false);
            List<OptionDao> subsInternal = new ArrayList<>();
            for(CatChildBean temp: childBean.getChilds()){
                if (StringUtils.isEmpty(temp.getCatNameShow())){//没有名字的时候
                    OptionDao optionDaoTwo = new OptionDao(temp.getCatId(), temp.getCatNoShow(),temp.getCatNoShow());
                    subsInternal.add(optionDaoTwo);
                }else{
                    OptionDao optionDaoTwo = new OptionDao(temp.getCatId(), temp.getCatNameShow(),temp.getCatNameShow());
                    subsInternal.add(optionDaoTwo);
                }
            }
            daoInternal.setGroupDao(filterDaoTwoInternal);
            daoInternal.setSubList(subsInternal);
            dataList.add(daoInternal);
        }

        return dataList;

    }





}
